package com.singzi.hospappo.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.singzi.hospappo.common.result.Result;
import com.singzi.hospappo.common.utils.MD5;
import com.singzi.hospappo.hosp.service.HospitalSetService;
import com.singzi.hospappo.model.hosp.HospitalSet;
import com.singzi.hospappo.hosp.service.HospitalSetService;
import com.singzi.hospappo.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院管理")
@RestController
@CrossOrigin
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {



    @Autowired
    private HospitalSetService hospitalSetService;

    //医院设置CURD方法

    //1 查询所有医院设置
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("/findAll")//查询
    public Result<List<HospitalSet>> findAllHospitalSet(){
//        try {
//            int a = 100/0;
//        }catch (Exception e){
//            throw new AppoException("越界失败", 201);
//        }
////        int a = 100/0;

        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        return Result.ok(hospitalSetList);
    }

    //2 删除医院设置
    @ApiOperation(value = "删除医院设置")
    @DeleteMapping("{id}")
    public Result<Boolean> removeSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag) return Result.ok();
        else return Result.fail();
    }

    //3 条件查询带分页
    @ApiOperation(value = "分页查询")
    @PostMapping("findPageHospitalSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findPageHospitalSet(@PathVariable long current,
                                                         @PathVariable long limit,
                                                         @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        // create page object, pass the current page, the numbers of record of every page
        Page<HospitalSet> hospitalSetPage = new Page<>(current, limit);

        //construct condition

        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hoscode)){
            wrapper.like("hoscode", hospitalSetQueryVo.getHoscode());
        }
        //call method implements paging query
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(hospitalSetPage, wrapper);
        return Result.ok(pageHospitalSet);
    }

    //4 添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("addHospSet")
    public Result<Boolean> addHospSet(@RequestBody(required = false) HospitalSet hospitalSet){
        hospitalSet.setStatus(1);

        Random random = new Random();
        String encrypt = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(encrypt);

        boolean save = hospitalSetService.save(hospitalSet);

        if(save){
            return Result.ok();
        }
        else return Result.fail();
    }

    //5 follow id to find a hospital
    @GetMapping("getHospital/{id}")
    public Result<HospitalSet> getHosp(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        return Result.ok(hospitalSet);
    }

    //6 modify the information of hospital
    @PostMapping("updateHosp")
    public Result<Boolean> updateHosp(@RequestBody(required = false) HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }
        else {
            return Result.fail();
        }
    }

    //7 batch remove hospital
    @DeleteMapping("batchRemove")
    public Result<Boolean> removeBatchHosp(@RequestBody List<Long> idList){
        boolean flag = hospitalSetService.removeByIds(idList);
        if(flag){
            return Result.ok();
        }
        else {
            return Result.fail();
        }
    }
    //8 医院锁定和解锁
    @PutMapping("lockHospSet/{id}/{status}")
    public Result<Boolean> lockHosp(@PathVariable Long id,
                           @PathVariable Integer status){

        HospitalSet hospitalSet = hospitalSetService.getById(id);

        hospitalSet.setStatus(status);

        hospitalSetService.updateById(hospitalSet);
        return Result.ok();

    }
    //9 send signKey
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        return Result.ok();
    }

}
